package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class NursingProjectServiceImpl implements NursingProjectService {
    final NursingProjectMapper nursingProjectMapper;
    final NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 分页条件查询护理项目
     *
     * @param dto 护理项目分页条件查询Dto
     * @return {@code PageBean<NursingProjectVo> }
     * @author YiSheep
     */
    @Override
    public PageBean<NursingProjectVo> pageQuery(NursingProjectPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingProjectVo> page = nursingProjectMapper.pageQuery(dto);
        PageBean<NursingProjectVo> pageBean = PageBean.of(page, NursingProjectVo.class);
        return pageBean;
    }

    /**
     * 新增护理项目
     *
     * @param dto 护理项目分页条件查询Dto
     * @author YiSheep
     */
    @Override
    public void addNursingProject(NursingProjectDto dto) {
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setCreateBy(1671403256519078138L);
        nursingProject.setUpdateBy(1671403256519078138L);
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    /**
     * 根据id查询护理项目
     *
     * @param id 护理项目ID
     * @return {@code NursingProjectVo }
     * @author YiSheep
     */
    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        NursingProjectVo vo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return vo;
    }

    /**
     * 修改护理项目
     *
     * @param dto 护理项目Dto
     * @author YiSheep
     */
    @Override
    public void updateNursingProject(NursingProjectDto dto) {
        if (dto.getStatus() == 1){
            throw new BaseException(BasicEnum.UPDATE_PROJECT_STATUS_FAIL);
        }
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(1671403256519078138L);
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    /**
     * 启用/禁用护理项目
     *
     * @param id     护理项目ID
     * @param status 状态
     * @author YiSheep
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingProject nursingProject = NursingProject.builder().id(id).status(status).build();
        //通过budiler方法，灵活构建一个对象
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    /**
     * 删除护理项目
     *
     * @param id 护理项目ID
     * @author YiSheep
     */
    @Override
    public void deleteById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        //判断护理项目的状态是否启用，如果启用，抛出异常
        if (nursingProject.getStatus() == 1) {
            throw new BaseException(BasicEnum.DELETE_PROJECT_STATUS_FAIL);
        }
        //判断该护理项目是否关联护理计划
        List<NursingProjectPlan> projectPlans = nursingProjectPlanMapper.getByProjectId(id);
        if (CollUtil.isNotEmpty(projectPlans)) {
            throw new BaseException(BasicEnum.DELETE_PROJECT_REF_FAIL);
        }
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有护理项目
     *
     * @return {@code List<NursingProject> }
     * @author YiSheep
     */
    @Override
    public List<NursingProject> getAllNursingProject() {
        return nursingProjectMapper.getAllNursingProject();
    }
}
